package weight;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.common.R;
import com.labo.kaji.swipeawaydialog.support.v4.SwipeAwayDialogFragment;

/**
 * 通用对话框 yjl
 */
public class ComDialog extends SwipeAwayDialogFragment {
    private TextView mLeftView, mRightView, mText, mTitle;
//    private LinearLayout mTitlelayout;
    private View rootView;
    private boolean outSidedismiss = false;//点击外面是否会消失
    private boolean isRightSubmit=true;
    private Context mContext;

    public static ComDialog newInstance(Context context) {
        ComDialog adf = new ComDialog();
        adf.rootView = LayoutInflater.from(context).inflate(R.layout.dialog_com, null);
        adf.init(adf.rootView);
        return adf;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.dialog_contact);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//        rootView = inflater.inflate(R.layout.dialog_com, container);
        getDialog().setCanceledOnTouchOutside(outSidedismiss);
        if(!outSidedismiss){
            getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
        }
        return rootView;
    }

    public void init(View rootView) {
        mLeftView = (TextView) rootView.findViewById(R.id.dialog_contact_cancel);
        mRightView = (TextView) rootView.findViewById(R.id.dialog_contact_submit);
        mText = (TextView) rootView.findViewById(R.id.dialog_contact_text);
        mTitle = (TextView) rootView.findViewById(R.id.dialog_contact_title);
//        mTitlelayout = (LinearLayout) rootView.findViewById(R.id.dialog_contact_titlelayout);

        if (null != mLeftView) {
            mLeftView.setOnClickListener(cancelClick);
        }

    }

    /**
     * diaog标题
     *
     * @param title
     */
    public void setTitle(String title) {
        if (null != mTitle) {
            mTitle.setText(title);
//            mTitlelayout.setVisibility(View.VISIBLE);
        }
    }

    /**
     * diaog提示语
     *
     * @param text
     */
    public void setText(String text) {
        if (null != mText) {
            mText.setText(text);
        }
    }

    /**
     * diaog提示语
     *
     * @param text
     * @param size 字体大小
     */
    public void setText(String text, int size) {
        if (null != mText) {
            mText.setText(text);
            mText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size);
        }
    }

    /**
     * 确认监听事件
     *
     * @param mSubmitListen 确认的监听
     */
    public void setSubmit(View.OnClickListener mSubmitListen) {
       /* if (null == mSubmitListen) {
            if (null != mSubmit) {
                mSubmit.setOnClickListener(cancelClick);
            }
        } else if (null != mSubmit) {
            mSubmit.setOnClickListener(mSubmitListen);
        }*/
       if(isRightSubmit){
           mRightView.setOnClickListener(mSubmitListen);
       }else {
           mLeftView.setOnClickListener(mSubmitListen);
       }
    }

    public void setSubmitText(String str) {
        if(isRightSubmit){
            mRightView.setText(str);
        }else{
            mLeftView.setText(str);
        }
    }

    public void setCancelText(String str) {
        if(isRightSubmit){//左侧是取消按钮
            mLeftView.setText(str);
        }else{//右侧是
            mRightView.setText(str);
        }
    }

    /**
     * 取消监听事件
     *
     * @param mCancelListen 返回监听
     */
    public void setCancel(View.OnClickListener mCancelListen) {
        if(isRightSubmit){
            mLeftView.setOnClickListener(mCancelListen);
        }else{
            mRightView.setOnClickListener(mCancelListen);
        }
        /*if (null == mCancelListen) {
            if (null != mCancel) {
                mCancel.setOnClickListener(cancelClick);
            }
        } else if (null != mCancel) {
            mCancel.setOnClickListener(mCancelListen);
        }*/
    }

    /**
     * 去除取消按钮
     */
    public void setCancelGone() {
        if (null != mLeftView) {
            mLeftView.setVisibility(View.GONE);
            mRightView.setBackgroundResource(R.drawable.btn_all_bg);
        }
    }

    /**
     * 左边的是确认按钮
     */

    public void setLeftSubmit(Context mContext){
        mRightView.setTextColor(ContextCompat.getColor(mContext,R.color.c_333333));
        mLeftView.setTextColor(ContextCompat.getColor(mContext,R.color.c_0396EB));
        isRightSubmit=false;
    }

    /**
     * 设置弹框外消失
     *
     * @param boo
     */
    public void setCanceledOnTouchOutside(Boolean boo) {
        outSidedismiss = boo;
    }

    private View.OnClickListener cancelClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ComDialog.this.dismiss();
        }
    };

    @Override
    public void show(FragmentManager manager, String tag) {
        //这里直接调用show方法会报java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();
    }
}
